import md5 from "md5";

const host = "https://xfmz.linpingdata.com";

const appKey = "bLpa&Ly1kP1k4ghy";
const appSecret = "gqu$DJf7zfKa8RKw";
const phoneNumber = "15666666666";
const timestamp = Date.now().toString();
// 随机字母
const nonce = "xxxxxxxx";

const generateSign = (params, appSecret) => {
  const keys = Object.keys(params).sort();
  const str = keys.map((key) => `${key}=${params[key]}`).join("&");
  return md5(str + "&key=" + appSecret);
};

const formdata = new FormData();
formdata.append("appKey", appKey);
formdata.append("phoneNumber", phoneNumber);

const headers = {
  timestamp: timestamp,
  sign: generateSign(
    {
      appKey,
      phoneNumber,
      nonce,
      timestamp,
    },
    appSecret,
  ),
  nonce: nonce,
};

const auth = () => {
  fetch(`${host}/prod-api/auth/third/login`, {
    method: "POST",
    headers: headers,
    body: formdata,
  })
    .then((res) => res.json())
    .then((data) => {
      const { access_token } = data?.data ?? {};
      if (access_token) {
        console.log(`${host}?callback_token=${access_token}`);
      } else {
        console.log(data);
      }
    })
    .catch((err) => {
      console.log(err);
    });
};
auth();
